package com.itheima.springboottlias.service.impl;


import Pojo.Dept;
import com.itheima.springboottlias.anno.LogOperation;
import com.itheima.springboottlias.exception.NotAllowedException;
import com.itheima.springboottlias.mapper.DeptMapper;
import com.itheima.springboottlias.mapper.EmpMapper;
import com.itheima.springboottlias.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/*
* 部门业务处理实现类：处理部门相关业务逻辑
* */
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Dept> selectAll() {
        return deptMapper.queryAll();
    }

    @Override
    @LogOperation
    public void saveDept(Dept dept) {
        //1.补充基础信息：创建时间。更新时间
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        //2.调用mapper完成添加
        deptMapper.saveDept(dept);
    }

    @Override
    @LogOperation
    public void removeById(Integer id) {
        //0.删除之前，先查询出要删除的部门是否有员工
        //1.如果部门有员工，则不允许删除
        if (empMapper.queryByDeptId(id) != 0) {
            //部门有员工，不允许删除
            throw new NotAllowedException("部门有员工，不允许删除");
        }
        //2.如果部门没有员工，则允许删除
        //3.调用mapper完成删除
        deptMapper.removeById(id);
    }

    @Override
    @LogOperation
    public Dept findById(Integer id) {
        return deptMapper.findById(id);
    }

    @Override
    @LogOperation
    public void update(Dept dept) {
        //1.同步更新时间
        dept.setUpdateTime(LocalDateTime.now());
        //2.调用mapper完成修改
        deptMapper.update(dept);
    }
}
